home *** CD-ROM | disk | FTP | other *** search
/ The Ultimate Window Set -…Games & Quality Programs / The Ultimate Window Set - 250 Games & Quality Programs.iso / win / pro289 / readme.pok < prev    next >
Text File  |  1993-12-17  |  10KB  |  247 lines

  1. Visual Basic Video Poker By Dan Mullin, 72644,2423
  2.  
  3. In this archive you will find both the source code and a compiled version of 
  4. Windows Video Poker, along with 53 .BMP files. No, it will not pay you back if 
  5. you win but, then again, you don't have to pump it full of dollar tokens either!
  6.  
  7. As simple as this game is, I loaded it for a different reason then to simply 
  8. provide a little midday diversion. If you enjoy card games like I do, you will 
  9. find some very important and worthwhile procedures included in this code.
  10.  
  11.  
  12. SHUFFLING:
  13.  
  14. The typical routine for creating a deck of shuffled cards is to generate an 
  15. array of 52 unique random numbers ranging from 0 to 51, i.e.:
  16.  
  17. Dim Deck(51) As Integer
  18. Dim Collision As Integer
  19.   'Collision is the detection of a repeated number
  20.  
  21. Randomize
  22. For Index = 0 To 51
  23.   Collision = True
  24.     Do Until Not Collision
  25.       Deck(Index) = Int(51 * Rnd + 1)
  26.       Collision = False
  27.       If Index > 0 Then
  28.         For Index2 = 0 To Index - 1
  29.           If Deck(Index2) = Deck(Index) Then
  30.             Collision = True
  31.           End If
  32.         Next Index2
  33.       End If
  34.     Loop
  35. Next Index
  36.  
  37. You use this number as an index to find the specific card, i.e., 0, A of Clubs,
  38. 1, 2 of Clubs...50, Q of Spades, 51, K of Spades.
  39.  
  40. What's the problem? As you can see, the first few numbers are generated fairly 
  41. quickly. However, as the size of the deck increases, the search for a repeated 
  42. number, a collision, gets longer and the possiblities of generating a repeated 
  43. number becomes greater as more numbers are entered into the array and fewer 
  44. unique numbers are left!
  45.  
  46. The SHUFFLE procedure in VPOK.BAS approaches this problem from a different 
  47. angle. When you open a new deck of cards they are all in order. You then mix 
  48. them up. The SHUFFLE routine also places all of the cards in order. The record 
  49. for a card contains its value (1=A, 13=K), its suit(0=Clubs, 1=Diamonds, 
  50. 2=Hearts, 3=Spades), and its number in the deck (0=A Of Clubs, 1=2 Of Clubs...
  51. 50=Q Of Spades, 51=K Of Spades). It also contains a variable we will call POS. 
  52. POS is assigned a random value as each card is assigned its ordered values. 
  53. We then have 52 cards in order, each holding a totally unordered POS variable. 
  54. We then sort the deck by the POS variable, creating a deck of unordered cards 
  55. with their POS variables in order! What if a POS value is repeated? The sort 
  56. routine doesn't care. It simply leaves the two cards in order. Ta-Da, the deck 
  57. is shuffled!
  58.  
  59. You will notice that we add 1 to the value of the card, i.e., A<>0, A=1 and 
  60. 2<>1,2=2. Why? If you create a game that uses scoring for the cards the scoring
  61. normally is consistant with the card value. A 3 usually equals 3 points!
  62.  
  63. Some games may require more than one deck. NO PROBLEM! Just increase your 
  64. DeckSize to 103, 154, whatever, and place multiple sets of ordered decks into 
  65. the array using MOD 52 as shown.
  66.  
  67.  
  68. CARD GRAPHICS:
  69.  
  70. I have included a set of .BMP files for each card in the deck, plus one card 
  71. back. If they look familiar I will confess that I copied them out of the .EXE 
  72. file for Solitaire (Hope I didn't violate copyrights!) When I create a cardgame
  73. one of the first things I do is create a control array of 53 picture boxes, 
  74. i.e., Picture1(0)-Picture1(52), and load all of the bitmaps into them in order 
  75. according to the card's number in the deck (0=A Of Clubs...51=K Of Spades, 
  76. 52=card back ). I keep this array of picture boxes hidden so they are not 
  77. displayed on the form. This makes the final program bigger but speeds and 
  78. simplifies the process of displaying the cards. Once loaded, all I have to do 
  79. is pass the Picture property of the deck card to the picture box where I want 
  80. to display the card. For example:
  81.  
  82. Picture2 is a visible picture box
  83. Picture1() is the invisible array of picture boxes containing the deck.
  84.  
  85. Picture2.Picture = Picture1(3).Picture
  86.  
  87. This will copy the graphic for the fourth card (we start at 0) in the deck to 
  88. the picture box where I want the card to be displayed.
  89.  
  90. In your code it will typically look like this:
  91.  
  92. Picture2.Picture = Picture1(Deck(CardYouWant).CardNum).Picture
  93.  
  94. You DO NOT have to make more than one array of Picture1() if your program uses 
  95. more than one deck! The shuffle routine produced multiple copies of the same 
  96. deck and, though the positions in the array go from 0 to DeckSize, the CardNum
  97. values go from 0 to 51 for the first deck, then repeat 0 to 51 for each deck 
  98. that follows.
  99.  
  100.  
  101. PARSING A HAND:
  102.  
  103. Though this procedure in VPOK.FRM is poker specific, it may give you some ideas
  104. on how to efficiently analyze a hand. We basically have 9 possible winning hands
  105. in Video Poker. We assign an integer to these winning hands as follows and 
  106. return it to see if we won:
  107.  
  108. Royal Flush = 9
  109. Straight Flush = 8
  110. Four Of A Kind = 7
  111. Full House = 6
  112. Flush = 5
  113. Straight = 4
  114. Three Of A Kind = 3
  115. Two Pair = 2
  116. Jacks Or Better = 1
  117.  
  118. First we check for a Flush, i.e., all cards of the same suit. This is easy! Set
  119. Flush = True, then cycle through the hand to see if any of the card suits are 
  120. not equal. If not equal, change Flush = False.
  121.  
  122. Next we check for a Straight, i.e., all cards in numerical order. To simplify 
  123. this check (and the rest of the checks) we sort the hand according to the card 
  124. value (1=A, 13=K). So we can manipulate these values without corrupting the 
  125. hand, we copy them over to a temporary array, V(4), which holds only these 
  126. values. We don't care about the suit anymore. All we are concerned with is their
  127. numerical values. See the Hand_Sort procedure.
  128.  
  129. Since our cards are now in numerical order, a Straight can be detected if each 
  130. card has a value one less than the next card in the hand. We set Straight = True
  131. and loop through comparing values, CardVal(Index) + 1 = CardVal(Index + 1). 
  132. If any of these checks fail, we set Straight = False.
  133.  
  134. We have one special case for a Straight. The Ace can either be before a 2 or 
  135. after a King. Since an Ace has a value of 1 it won't be placed after the King 
  136. when sorted and we could miss a 10,J,Q,K,A straight. Ahh, but we have the V(4) 
  137. array that we can change without screwing up our original hand. We check to see
  138. if the first card is a "1", i.e., a "low" Ace. If so, we copy all of the cards 
  139. down one position and make the last card a 14, a "high" Ace. We then check for 
  140. a Straight once more. If we have a 10,J,Q,K,A straight the card values will be 
  141. 10,11,12,13,14 and it will be detected.
  142.  
  143. We then look for Straight Flushes and Royal Flushes. If Straight = True and 
  144. Flush = True and the first card, V(0), is a 10, we have a Royal Flush. Remember
  145. that the Ace will be at the top in a 10,J,Q,K,A straight. If the first card is
  146. not a 10, we have a Straight Flush. Otherwise, we have a Straight, a Flush, or 
  147. Nothing. If we have Nothing at this point we go on.
  148.  
  149. We reassign the original card values back to V(4) since we may have changed them
  150. when doing the check for a 10-A straight. The next chunk of code may seem 
  151. convoluted but it is actually a very efficient method of checking for the 
  152. remaining possible winning hands.
  153.  
  154. We take our copy of the hand, V(4), and look at the first four cards in order.
  155. If the card equals the next card in the hand, we keep its value. Otherwise we 
  156. make it 0. We then make the last card = 0. For example:
  157.  
  158. Hand      23359
  159. Result    03000
  160.  
  161. Hand      34499
  162. Result    04090
  163.  
  164. We create a second array, P(2), of three integers and a counter called SameVal. 
  165. We loop through our V(4) array again. If the card <> 0 then we copy it to 
  166. P(SameVal) and increment SameVal by one. From the above example:
  167.  
  168. Result    04090
  169. P(0),P(1) 49
  170. SameVal   2
  171.  
  172. Believe me, it's easier than trying to cover every possible combination looking
  173. at the full hand. This way we toss all of the cards that can have no impact on 
  174. the result and concentrate on the cards that do have an effect! We now have 
  175. everything we need to determine all of the other winning combinations. Look at 
  176. these examples:
  177.  
  178.  
  179. Hand      24578
  180. Result    00000
  181. SameVal   0
  182.  
  183. If SameVal = 0 then we have Nothing.
  184.  
  185.  
  186. Hand      23379
  187. Result    03000
  188. P(0)      3
  189. SameVal   1
  190.  
  191. If SameVal = 1 then we have one Pair. If P(0) > 10 then the pair is Jacks Or 
  192. Better.
  193.  
  194.  
  195. Hand      45556
  196. Result    05500
  197. P(0),P(1) 55
  198. SameVal   2
  199.  
  200. If SameVal = 2 and P(0) = P(1) then we have Three Of A Kind.
  201.  
  202.  
  203. Hand      33699
  204. Result    30090
  205. P(0),P(1) 39
  206. SameVal   2
  207.  
  208. If SameVal = 2 and P(0) <> P(1) then we have Two Pair.
  209.  
  210.  
  211. Hand            24444
  212. Result          04440
  213. P(0),P(1),P(2)  444
  214. SameVal         3
  215.  
  216. If SameVal = 3 and P(0) = P(1) = P(2) we have Four Of A Kind
  217.  
  218.  
  219. Hand            33555
  220. Result          30550
  221. P(0),P(1),P(2)  355
  222. SameVal         3
  223.  
  224. If SameVal = 3 and P(0), P(1), and P(2) are not all equal we have a Full House.
  225.  
  226.  
  227. Not Bad, Ehhhh?
  228.  
  229.  
  230. SUMMARY:
  231.  
  232. Along with a fully functional game, you now have some of the tools needed to 
  233. create your own card games. If you use these procedures all I ask is that you 
  234. include a little note in your splash screen or code like:
  235.  
  236. "Shuffle" Procedure By Dan Mullin
  237.  
  238. It simply lets me know that someone finds this work of value. And let me know if
  239. you DO create a program using these procedures. My ID is 72644,2423.
  240.  
  241. Don't distribute this code or the complete program for money! Just give it away! 
  242.  
  243. Bang on it, beat it, change it, screw it up! I don't care! HAVE FUN!
  244.  
  245. Dan Mullin
  246. 72644,2423
  247.